package General;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Manuel
 */
public class RequestMaker {

    public static String getPage(String u){

        String page ="";
        try {
            URL url = new URL(u);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                page = page + str;
            }
            /*
            HTTP http;
            try {
            URL url = new URL(u);
            http = new HTTPSocket();
            http.send(url.toString(), null);
            } catch (Exception ex) {
            System.out.println("Could not retrieve page: "+u);
            return "";
            }
            String page = http.getBody();
            return page;
             */
        } catch (Exception ex) {
            System.err.println("Cannot reach the requested page "+u+ " wrong url or missing connection");
        }
            return page;
 }

    public static String getMap(String u){
        String result = "";
        String line;
        try {
            URL url = new URL(u);
            InputStream is = url.openStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader r = new BufferedReader(isr);
            do {
                line = r.readLine();
                if (line != null) {
                    result = result + line;
                }
            } while (line != null);
        } catch (Exception ex) {
            System.err.println("Could not retrieve sitemap: "+u+" wrong url or missing connection");
            result = "";
        }
        return result;
    }



    public static String storeFile(String urlString, String destination){
    try
      {
          File dir = new File(destination);
          if(!dir.exists() || !dir.isDirectory())
              dir.mkdir();
          String destFile = urlString;
          while(destFile.contains("/")){
                  destFile = destFile.substring(destFile.indexOf("/")+1);
          }
          destFile = destination +"/"+destFile;
          URL url  = new URL(urlString);
          URLConnection urlC = url.openConnection();
          // Copy resource to local file, use remote file
          // if no local file name specified
          InputStream is = url.openStream();
          Date date=new Date(urlC.getLastModified());
          File outputFile = new File(destFile);
          FileOutputStream fos=null;
          fos = new FileOutputStream(outputFile);
          int oneChar, count=0;
          while ((oneChar=is.read()) != -1){
             fos.write(oneChar);
             count++;
          }
          is.close();
          fos.close();
          System.out.println("Downloaded "+destFile);
          return destFile;
      }
      catch (Exception e){
          System.err.println(e.toString());

          return "";
      }
  }


}
